package com.example.compose

import android.os.CountDownTimer
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp


@Composable
fun CountDownTimerExample() {
    var timer by remember {
        mutableLongStateOf(value = 0)
    }
    var expanded by remember {
        mutableStateOf(false)
    }

    val countDownTimer = object : CountDownTimer(10 * 1000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            timer = millisUntilFinished / 1000
        }

        override fun onFinish() {
            timer = 0
        }
    }

    DisposableEffect(key1 = "key") {
        countDownTimer.start()
        onDispose {
            countDownTimer.cancel()
        }
    }
    if (timer > 0) {
        OutlinedButton(onClick = { /*TODO*/ }, enabled = expanded) {
            Row {
                Text(text = "$timer")
                if (timer < 5) {
                    expanded = true
                    Text(text = " | 跳过")
                }
            }


        }
    }


}

@Preview
@Composable
private fun CountDownTimerPreview() {
    CountDownTimerExample()
}